<?php
	include_once("./dbcon.php");

	ini_set('max_execution_time', 604800);
	date_default_timezone_set('America/Argentina/Buenos_Aires');

	define("MAX_DAYS_TO_CRAWL", "14");
	define("MAX_EXECUTION_TIME", 25);
	define("CURRENT_URL", "http://www.pageposter.solo-dilo.com/crawl.php");

	function crawlLikes($post_id, $access_token) {
		global $mysqli;
		$like_count = 0;

		$url = "https://graph.facebook.com/".$post_id."/likes?fields=id&limit=1000&access_token=".$access_token;
		
		while (1) {
			$likes = json_decode(file_get_contents($url));
			$likesData = $likes->data;

			$like_count = $like_count + count($likesData);

			if (isset($likes->paging->next) == false)
				break;

			$url = $likes->paging->next;
		}

		$mysqli->query("UPDATE posts SET likes=".$like_count.", last_date_crawled='".date('Y-m-d H:i:s')."' WHERE post_id='".$post_id."'");
		
		return true;
	}

	function getAccessTokenQuantity() {
		global $mysqli;

		return $mysqli->query("SELECT COUNT(*) as count FROM access_tokens")->fetch_assoc()['count'];
	}

	function getAccessTokenRow($row) {
		global $mysqli;

		$query = $mysqli->query("SELECT value as count FROM access_tokens ORDER BY fb_id");
		$final_row = ($query->num_rows <= $row + 1) ? $query->num_rows - 1 : $row;
		for ($i = 0; $i <= $final_row; $i++) 
			$retval = $query->fetch_assoc();

		return $retval['value'];
	}

	function crawlAllPosts() {
		global $mysqli;
		global $start_time;
		global $current_start_time;

		$post_id_condition = (isset($_GET['continue_from_post_id'])) ? "AND id > ".$_GET['continue_from_post_id'] : "";

		$posts = $mysqli->query("SELECT id, post_id FROM posts WHERE DATEDIFF(now(), creation_date) < ".MAX_DAYS_TO_CRAWL." ".$post_id_condition." ORDER BY id ASC");

		$posts_per_token = (isset($_GET['posts_per_token'])) ? $_GET['posts_per_token'] : floor($posts->num_rows / getAccessTokenQuantity());
		$contents_crawled = (isset($_GET['contents_crawled'])) ? $_GET['contents_crawled'] : 0;
		$current_token_row = floor($contents_crawled/$posts_per_token);
		$prev_token_row = $current_token_row;
		$access_token = getAccessTokenRow($current_token_row);

		while ($row = $posts->fetch_assoc()) {
			crawlLikes($row['post_id'], $access_token);
			$contents_crawled++;
			$current_token_row = floor($contents_crawled/$posts_per_token);

			if ($prev_token_row != $current_token_row) {
				$prev_token_row = $current_token_row;
				$access_token = getAccessTokenRow($current_token_row);
			}

			if (microtime(true) - $current_start_time > MAX_EXECUTION_TIME) {
				echo CURRENT_URL."?continue_from_post_id=".$row['id']."&start_time=".$start_time."&contents_crawled=".$contents_crawled."&posts_per_token=".$posts_per_token;
				exit();
			}
		}
	}

	function computeWeights() {
		global $mysqli;
		// Set total likes for pages
		$mysqli->query("UPDATE pages pa SET pa.total_likes = ( SELECT SUM(po.likes) FROM posts po WHERE pa.page_id = po.page_id )");

		// Update url weight
		$mysqli->query("UPDATE urls u SET u.weight = (SELECT AVG(x.w) FROM (SELECT (SELECT sum(po.likes) / (pa.total_likes * count(*)) FROM posts po WHERE po.page_id = pa.page_id AND po.url_id = u.id) as w, u.id as url_id FROM pages pa, urls u) AS x WHERE x.url_id = u.id)");
	}

	function getGlobalParameter($name) {
		global $mysqli;
		$value = $mysqli->query("SELECT value FROM global_parameters WHERE name='".$name."'");

		if ($value->num_rows > 0)
			return $value->fetch_assoc()['value'];

		return null;
	}

	$start_time = ( isset($_GET['start_time']) ) ? $_GET['start_time'] : microtime(true);
	$current_start_time = microtime(true);
	crawlAllPosts();
	$crawl_time = round(microtime(true) - $start_time, 4);

	$start_time = microtime(true);
	computeWeights();
	$compute_time = round(microtime(true) - $start_time, 4);


	$posts_quantity = $mysqli->query("SELECT COUNT(*) as quantity FROM posts WHERE DATEDIFF(now(), creation_date) < ".MAX_DAYS_TO_CRAWL);
	$posts_quantity = $posts_quantity->fetch_assoc();

	$desc = "Crawling complete. Crawled posts: ".$posts_quantity['quantity'].". Crawl time elapsed: ".$crawl_time."s. Compute time elapsed: ".$compute_time."s";
	$mysqli->query("INSERT INTO log (description, creation_date) VALUES ( '".$desc."', '".date('Y-m-d H:i:s')."')");

	echo '0';
?>